﻿
var instance = axios.create({ timeout: 1000 * 60, baseURL: 'http://192.168.0.110:8031' });
// 设置post请求头
// instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';  (可以不在这里封装）
// 拦截器
const { message } = antd
var params = {
    messageTest:{
        value:''
    }
}
params.messageTest.value = message
params.messageTest.value.config({
    top: `10vh`,
    duration: 7,
    maxCount: 1,
    rtl: true,
});
instance.interceptors.request.use(
    config => {
        const token = window.sessionStorage.getItem('token');
        token && (config.headers.Authorization = token);
        return config;
    },
    error => Promise.error(error)
)
// 响应拦截器
instance.interceptors.response.use(

    // 请求成功
    res => {
        return new Promise((resolve, reject) => {
            let successCodeList = [-100, 100, 200]

            if (successCodeList.includes(res.data.Code)) {
                if (res.data.Code == -100) {
                  return  params.messageTest.value.info(res.data.Msg || '网络繁忙');
                }
                resolve(res.data)
            } else {
                if (res.data.Code == -205) {

                    params.messageTest.value.info(res?.Msg || res?.Data?.Msg || '认证失败,请重新登录');
                    setTimeout(() => {
                        window.location.href='../Account/Login'
                    },2000)
                } else {
                    reject(res.data)
                    params.messageTest.value.error('网络繁忙，请联系管理员');
                }
            }
        })
    },

    // 请求失败
    error => {
        //const { response } = error;
        params.messageTest.value.error('网络繁忙，请联系管理员');
        return Promise.reject('网络繁忙，请联系管理员')

    });
export default instance